package cn.hahah.module.blog.controller.admin.userdialogue;

import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import cn.hahah.module.blog.service.users.UsersService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.userdialogue.vo.*;
import cn.hahah.module.blog.dal.dataobject.userdialogue.UserDialogueDO;
import cn.hahah.module.blog.service.userdialogue.UserDialogueService;

@Tag(name = "管理后台 - 用户对话")
@RestController
@RequestMapping("/blog/user-dialogue")
@Validated
public class UserDialogueController {

    @Resource
    private UserDialogueService userDialogueService;

    @Resource
    private UsersService usersService;

    @PostMapping("/create")
    @Operation(summary = "创建用户对话")
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue:create')")
    public CommonResult<Long> createUserDialogue(@Valid @RequestBody UserDialogueSaveReqVO createReqVO) {
        return success(userDialogueService.createUserDialogue(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户对话")
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue:update')")
    public CommonResult<Boolean> updateUserDialogue(@Valid @RequestBody UserDialogueSaveReqVO updateReqVO) {
        userDialogueService.updateUserDialogue(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户对话")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue:delete')")
    public CommonResult<Boolean> deleteUserDialogue(@RequestParam("id") Long id) {
        userDialogueService.deleteUserDialogue(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户对话")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue:query')")
    public CommonResult<UserDialogueRespVO> getUserDialogue(@RequestParam("id") Long id) {
        UserDialogueDO userDialogue = userDialogueService.getUserDialogue(id);
        return success(BeanUtils.toBean(userDialogue, UserDialogueRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户对话分页")
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue:query')")
    public CommonResult<PageResult<UserDialogueRespVO>> getUserDialoguePage(@Valid UserDialoguePageReqVO pageReqVO) {
        PageResult<UserDialogueDO> pageResult = userDialogueService.getUserDialoguePage(pageReqVO);
        PageResult<UserDialogueRespVO> result = BeanUtils.toBean(pageResult, UserDialogueRespVO.class);
        for (UserDialogueRespVO userDialogueRespVO : result.getList()) {
            UsersDO usersDO = usersService.getUsers(userDialogueRespVO.getUserId());

            if (usersDO != null){
                userDialogueRespVO.setUserName(usersDO.getUsername());
            }
        }
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户对话 Excel")
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserDialogueExcel(@Valid UserDialoguePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserDialogueDO> list = userDialogueService.getUserDialoguePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户对话.xls", "数据", UserDialogueRespVO.class,
                        BeanUtils.toBean(list, UserDialogueRespVO.class));
    }

}